package com.qqbot.core.message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qqbot.core.message.support.*;

import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * 消息链
 *
 * @author changwenpeng
 * @date 2022年5月9日
 */
public class MessageChain extends ArrayList<Message> {

    private static final long serialVersionUID = 1L;

    @Override
    public String toString() {
        return this.stream().map(Object::toString).collect(Collectors.joining());
    }

    public String toMessageString() {
        return this.stream().map(Message::toMessageString).collect(Collectors.joining(",", "[", "]"));
    }

    public MessageChain at(long qq) {
        this.add(new AtMessage(String.valueOf(qq)));
        return this;
    }

    public MessageChain atAll() {
        this.add(new AtMessage("all"));
        return this;
    }

    public MessageChain text(String message) {
        this.add(new TextMessage(message));
        return this;
    }

    public MessageChain image(String file) {
        this.add(new ImageMessage(file));
        return this;
    }

    public MessageChain reply(int messageId) {
        this.add(new ReplyMessage(messageId));
        return this;
    }

    public MessageChain reply(int messageId, String text) {
        this.add(new ReplyMessage(messageId, text));
        return this;
    }

    public MessageChain record(String file) {
        this.add(new RecordMessage(file));
        return this;
    }

    public MessageChain face(String id) {
        this.add(new FaceMessage(id));
        return this;
    }

    public MessageChain video(String file) {
        this.add(new VideoMessage(file));
        return this;
    }

    public MessageChain music(String id, String type) {
        this.add(new MusicMessage(id, type));
        return this;
    }

    public MessageChain music(String type, String subType, String url, String audio, String title, String content, String image) {
        this.add(new MusicMessage(type, subType, url, audio, title, content, image));
        return this;
    }

    public String getText() {
        String jsonMsg = this.stream().map(Message::toMessageString).collect(Collectors.joining(",", "[", "]"));
        JSONArray jsonArray = JSONArray.parseArray(jsonMsg);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject job = jsonArray.getJSONObject(i);
            String type = job.getString("type");
            if ("text".equals(type)) {
                JSONObject jsonData = job.getJSONObject("data");
                sb.append(jsonData.getString("text").replaceAll("\\s*", ""));
            }
        }
        return sb.toString();
    }

    public String getImage() {
        String jsonMsg = this.stream().map(Message::toMessageString).collect(Collectors.joining(",", "[", "]"));
        JSONArray jsonArray = JSONArray.parseArray(jsonMsg);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject job = jsonArray.getJSONObject(i);
            String type = job.getString("type");
            if ("image".equals(type)) {
                JSONObject jsonData = job.getJSONObject("data");
                sb.append(jsonData.getString("url").replaceAll("\\s*", ""));
            }
        }
        return sb.toString();
    }

    public String getTextAndAt(){
        String jsonMsg = this.stream().map(Message::toMessageString).collect(Collectors.joining(",", "[", "]"));
        JSONArray jsonArray = JSONArray.parseArray(jsonMsg);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject job = jsonArray.getJSONObject(i);
            String type = job.getString("type");
            if ("text".equals(type)) {
                JSONObject jsonData = job.getJSONObject("data");
                sb.append(jsonData.getString("text").replaceAll("\\s*", ""));
            }
            if ("at".equals(type)) {
                JSONObject jsonData = job.getJSONObject("data");
                sb.append(jsonData.getString("qq").replaceAll("\\s*", ""));
            }
        }
        return sb.toString();
    }

    public boolean hasAt(){
        String jsonMsg = this.stream().map(Message::toMessageString).collect(Collectors.joining(",", "[", "]"));
        JSONArray jsonArray = JSONArray.parseArray(jsonMsg);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject job = jsonArray.getJSONObject(i);
            String type = job.getString("type");
            if ("at".equals(type)) {
                return true;
            }
        }
        return false;
    }

    public MessageChain copy() {
        MessageChain messageChain = new MessageChain();
        messageChain.addAll(
                this.stream().map(message -> MessageTypeHandle.getMessage(JSON.parseObject(message.toMessageString())))
                        .collect(Collectors.toList()));
        return messageChain;
    }

}
